﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace Excel2Word
{
    public partial class FormStart : Form
    {
        public FormStart()
        {
            InitializeComponent();
            AppStore.mainForm = this;
        }

        /// <summary>
        /// 打开本地目录，选择一个EXCEL文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Excel 2007 Files(*.xlsx)|*.xlsx|Excle 2003 Files(*.xls)|*.xls";
            fd.FilterIndex = 1;
            fd.Title = "请选择EXCEL数据源文件";
            fd.Multiselect = false;
            fd.ReadOnlyChecked = false;

            if (fd.ShowDialog() == DialogResult.OK)
            {
                if (fd.FileName != null)
                {
                    AppStore.targetWorkbook = ExcelOperation.GetWorkBookByFullName(fd.FileName,true);
                    FrmDefineExcelSource fdes = new FrmDefineExcelSource();
                    fdes.Show();
                    this.Hide();
                }
            }
            
        }

        private void btnDefineWord_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Word 2007 Files(*.docx)|*.docx|Word 2003 Files(*.doc)|*.doc";
            fd.FilterIndex = 1;
            fd.Title = "请选择WORD模板";
            fd.Multiselect = false;
            fd.ReadOnlyChecked = false;

            if (fd.ShowDialog() == DialogResult.OK)
            {
                if (fd.FileName != null)
                {
                    AppStore.targetDoc = WordOperation.GetDocByFullName(fd.FileName,true);
                    FrmDefineWordReport fdwwr = new FrmDefineWordReport(AppStore.targetDoc);
                    fdwwr.Show();
                    this.Hide();
                }
            }
        }

        /// <summary>
        /// 数据写入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btDataInput_Click(object sender, EventArgs e)
        {
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Excel 2007 Files(*.xlsx)|*.xlsx|Excle 2003 Files(*.xls)|*.xls";
            fd.FilterIndex = 1;
            fd.Title = "请选择EXCEL数据源文件";
            fd.Multiselect = false;
            fd.ReadOnlyChecked = false;

            if (fd.ShowDialog() == DialogResult.OK)
            {
                if (fd.FileName != null)
                {
                    var dataSourceWb = ExcelOperation.GetWorkBookByFullName(fd.FileName, true);

                    AppStore.deSerilize();

                    ExcelReadHelper erh = new ExcelReadHelper(dataSourceWb, AppStore.listEle);

                   var resultDic = erh.GetDataFromExcel();

                   var app = dataSourceWb.Application as Microsoft.Office.Interop.Excel.Application;
                   dataSourceWb.Close();
                   app.Quit();
                  
                   
                   var targetWb = WordOperation.GetDocByFullName(@"C:\Users\Lenovo\Desktop\测试WORD.docx");


                   //6、遍历文档中的变量，设置变量内容
                   foreach (var var in targetWb.Variables)
                   {
                       if (var.Name.StartsWith(Dics.WORD_FIELD_TAG))
                       {
                           var fieldName = var.Name.Substring(Dics.WORD_FIELD_TAG.Length);
                           if (resultDic.ContainsKey(fieldName))
                               var.Value = (string)resultDic[fieldName];
                       }
                   }

                    
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            
            
        }
    }
}
